package cn.hncu.booksManagementSystem.in.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.BMS_APP;
import cn.hncu.booksManagementSystem.book.business.ebi.BookEbi;
import cn.hncu.booksManagementSystem.book.business.factory.BookEbiFactory;
import cn.hncu.booksManagementSystem.book.vo.BookModel;
import cn.hncu.booksManagementSystem.in.vo.InDetailModel;

public class UpdatePanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private JTextField tfdSumNum;
	private JFrame mainFrame;
	private List<InDetailModel> details;
	private int index ;
	private InDetailModel oldInDetail;
	//注入bookEbi
	private BookEbi bookEbi = BookEbiFactory.getInstanceOfBookEbi();
	private JComboBox<BookModel> cbbxBook;
	private JLabel labUserName;
	/**
	 * Create the panel.
	 */
	public UpdatePanel(JFrame mainFrame , List<InDetailModel> details,int index) {
		this.mainFrame = mainFrame;
		this.details = details;
		this.index = index;
		this.oldInDetail = details.get(index);
		initContentPane();
		initData();
	}

	private void initData() {
		Collection<BookModel> books = bookEbi.getAll();
		for (BookModel book : books) {
			cbbxBook.addItem(book);
			if (book.getId().equals(oldInDetail.getBookId())) {
				cbbxBook.setSelectedItem(book);
			}
		}
		tfdSumNum.setText(oldInDetail.getSumNum()+"");
		labUserName.setText(BMS_APP.activeUser.getName());
	}

	private void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("订单明细");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(304, 42, 145, 38);
		add(label);
		
		cbbxBook = new JComboBox<BookModel>();
		cbbxBook.setMaximumRowCount(4);
		cbbxBook.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		cbbxBook.setBounds(238, 143, 400, 38);
		add(cbbxBook);
		
		JLabel label_1 = new JLabel("图书：");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		label_1.setBounds(145, 143, 95, 38);
		add(label_1);
		
		JLabel label_2 = new JLabel("购买数量：");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		label_2.setBounds(174, 214, 145, 38);
		add(label_2);
		
		tfdSumNum = new JTextField();
		tfdSumNum.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdSumNum.setColumns(10);
		tfdSumNum.setBounds(330, 214, 199, 38);
		add(tfdSumNum);
		
		JLabel label_3 = new JLabel("进货人：");
		label_3.setHorizontalAlignment(SwingConstants.CENTER);
		label_3.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		label_3.setBounds(174, 303, 145, 38);
		add(label_3);
		
		labUserName = new JLabel((String) null);
		labUserName.setHorizontalAlignment(SwingConstants.CENTER);
		labUserName.setForeground(Color.RED);
		labUserName.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		labUserName.setBounds(331, 303, 145, 38);
		add(labUserName);
		
		JButton btnUpdate = new JButton("修改");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//1收集 id(逻辑层补)，inId(逻辑层补)，bookId，sumNum，SumMoney(逻辑层补)
				String bookId = ((BookModel)cbbxBook.getSelectedItem()).getId();
				int sumNum = 0;
				String strSumNum = tfdSumNum.getText();
				try {
					sumNum = Integer.parseInt(strSumNum);
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(mainFrame, "进货数量格式错误");
					tfdSumNum.requestFocus();
					return;
				}
				if (sumNum<=0) {
					JOptionPane.showMessageDialog(mainFrame, "进货数量不能小于等于0");
					tfdSumNum.requestFocus();
					return;
				}
				//2组织
				InDetailModel inDetail = new InDetailModel();
				inDetail.setId(oldInDetail.getId());
				inDetail.setInId(oldInDetail.getInId());
				inDetail.setBookId(bookId);
				inDetail.setSumNum(sumNum);
				
				//3调用details.set()
				boolean boo = details.set(index, inDetail) != null;
				//4导向结果界面
				if (!boo) {
					JOptionPane.showMessageDialog(mainFrame, "修改失败");
				}
				mainFrame.setContentPane(new InPanel(mainFrame,details));
				mainFrame.validate();
			}
		});
		btnUpdate.setForeground(Color.BLUE);
		btnUpdate.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnUpdate.setBounds(204, 395, 113, 45);
		add(btnUpdate);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new InPanel(mainFrame,details));
				mainFrame.validate();
			}
		});
		btnBack.setForeground(Color.BLUE);
		btnBack.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnBack.setBounds(471, 395, 113, 45);
		add(btnBack);
	}
}
